'''
例如方程(满秩唯一解)：
    3x1+4x2−2x3=7
    4x1+x2+3x3=6
    x1+x2+7x3=5
'''
import sympy as sp

# sympy
a = sp.Matrix([[3, 4, -2], [4, 1, 3], [1, 1, 7]])
b = sp.Matrix([[7], [6], [5]])
x = sp.Symbol('x')
x0 = sp.symarray(x, 3).reshape(3, 1)
res = sp.solve(a * x0 - b)
print(res)

'''
例如方程(非满秩无穷多解)：
    x1-x2-x3+x4=0
    x1-x2+x3-3x4=1
    x1-x2-2x3+3x4=-1/2
'''
a = sp.Matrix([[1, -1, -1, 1], [1 ,-1,1,-3], [1,-1,-2,3]])
b = sp.Matrix([0,1,-1/2]).reshape(3,1)
x = sp.Symbol('x')
x0 = sp.symarray(x, 4).reshape(4, 1)
res = sp.solve(a * x0 - b)
# 求出通解
sp.pprint(res)
# 基础解系
sp.pprint(a.nullspace())
# 取x2=x4=0
x0[1]=0
x0[3]=0
res = sp.solve(a * x0 - b)
# 求出特解
sp.pprint(res)
print(res)
print(res[0])